#!/bin/bash

# QTrayNotify
# Copyright (C) 2010 Ivan Babushkin (jogrms@gmail.com)
#  
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


# The command is called the following way:
#   $events_command MSG IN jabber@id [file] (when receiving a message)
#   $events_command MSG OUT jabber@id       (when sending a message)
#   $events_command MSG MUC room_id [file]  (when receiving a MUC message)
#   $events_command STATUS X jabber@id      (new buddy status is X)
#   $events_command UNREAD N                (number of unread buddy buffers)

if [ "$1" = "MSG" ]; then
	if [ \( "$2" = "IN" \) -o \( "$2" = "MUC" \) ]; then
		if [ "$4" != "" ]; then
			echo -e "$3\r$4" > /tmp/qtraynotify.fifo &
		else
			echo -e "$3\r" > /tmp/qtraynotify.fifo &
		fi
	fi
fi
if [ "$1" = "UNREAD" ]; then
	num=`echo "$2" | grep -o "^[[:digit:]]* "`
	if [ $num = "0" ]; then
		echo -e "\rclear" > /tmp/qtraynotify.fifo &
	fi
fi

